<script setup>
import { Edit, Delete } from "@element-plus/icons-vue";

import { ref } from "vue";

const bookId = ref("");

const readerId = ref("");

//用户搜索时选中的状态
const state = ref("");

//控制还书弹窗
const dialogVisible = ref(false);

//借阅列表数据模型
const borrow = ref([
  {
    id: 5,
    readerId: "读者编号",
    bookId: "bookId",
    returnDate: "2023-11-20",
    status: 0,
    state1: false,
  },
]);

//分页条数据模型
const pageNum = ref(1); //当前页
const total = ref(20); //总条数
const pageSize = ref(10); //每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size;
  borrowList();
};
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num;
  borrowList();
};

import {
  borrowListService,
  checkBorrowService,
  addBorrowService,
  returnBookService,
} from "@/api/books.js";


//获取借阅列表数据
const borrowList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,

    readerId: readerId.value ? readerId.value : null,
    state: state.value ? state.value : null,
    bookId: bookId.value ? bookId.value : null,
  };
  let result = await borrowListService(params);

  //渲染视图
  total.value = result.data.total;
  borrow.value = result.data.items;

  //处理数据，状态
  for (let j = 0; j < borrow.value.length; j++) {
    if (borrow.value[j].status == 0) {
      borrow.value[j].status = "已还";
      borrow.value[j].state1 = "true";
    } else {
      borrow.value[j].status = "未还";
      borrow.value[j].state1 = false;
    }
  }
};

borrowList();

import { Plus } from "@element-plus/icons-vue";
//控制抽屉是否显示
const visibleDrawer = ref(false);
//定义变量，控制标题的展示
const title = ref("");


//添加表单数据模型
const borrowModel = ref({
  id: "",
  readerId: "",
  bookId: "",
});
//核验数据模型
const checkModel = ref({
  readerName: "",
  bookname: "",
  total: "",
  returnDate: "",
  returnDate1: "",
  balance: "",
  balance1: "",
});

const dbutt = ref(true)
const disabledButt = (rule, value, callback) => {
    dbutt.value = true;
}
//添加表单校验
const rules = {
  readerId: [
    { required: true, message: "请输入借阅人编号", trigger: "blur" },
    { min: 1, max: 10, message: "长度不超过10位非空字符", trigger: "blur" },
    { validator: disabledButt, trigger: 'change' }
  ],
  bookId: [
    { required: true, message: "请输入图书编号", trigger: "blur" },
    { min: 1, max: 10, message: "长度不超过10位非空字符", trigger: "blur" },
    { validator: disabledButt, trigger: 'change' }
  ],
  
};

//清空模型的数据
const clearDate = () => {
  //   bookModel.value = "";
  (borrowModel.value.id = ""),
    (borrowModel.value.readerId = ""),
    (borrowModel.value.bookId = ""),
    (checkModel.value.readerName = ""),
    (checkModel.value.bookname = ""),
    (checkModel.value.total = ""),
    (checkModel.value.returnDate = ""),
    (checkModel.value.returnDate1 = ""),
    (checkModel.value.balance = ""),
    (checkModel.value.balance1 = "");
};

const checkBorrow = async () => {

  let result = await checkBorrowService(borrowModel.value);
  checkModel.value = result.data;
};

//调用接口，添加表单
import { ElMessage, ElMessageBox } from "element-plus";
const addBorrow = async () => {

  if(checkModel.value.balance<0){
    ElMessage.error("余额不足");
    return
  }
  //调用接口
  let result = await addBorrowService(borrowModel.value);
  ElMessage.success(result.msg ? result.msg : "借书成功");

  //调用获取列表
  borrowList();

  visibleDrawer.value = false;
};

//展示弹窗表单

const returnBook = async () => {

  let params = {
    readerId: borrowModel.value.readerId,
    bookId: borrowModel.value.bookId,
    balance: checkModel.value.balance1,
  };
  //调用接口
  let result = await returnBookService(params);
  ElMessage.success(result.msg ? result.msg : "还书成功");

  //调用获取列表
  borrowList();

  dialogVisible.value = false;
};


const showDialog = (row) => {
  //数据拷贝
  borrowModel.value.id = row.id;
  borrowModel.value.readerId = row.readerId;
  borrowModel.value.bookId = row.bookId;
  checkBorrow();
      
};

const isAble = (row) => {
  return row.state1;
};
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>借阅管理</span>
        <div class="extra">
          <el-button
            type="primary"
            @click="
              visibleDrawer = true;
              clearDate();
            "
            >借书</el-button
          >
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->

    <el-form inline>
      <!-- <el-input v-model="input" placeholder="Please input" clearable /> -->
      <el-form-item label="借阅人编号：">
        <el-input v-model="readerId" placeholder="请输入借阅人编号" clearable />
      </el-form-item>

      <el-form-item label="图书编号：">
        <el-input v-model="bookId" placeholder="请输入图书编号" clearable />
      </el-form-item>

      <el-form-item label="状态：">
        <el-select placeholder="请选择" v-model="state">
          <el-option label="已还" value="0"></el-option>
          <el-option label="未还" value="1"></el-option>
        </el-select>
      </el-form-item>
      <!-- <br>  -->
      <el-form-item>
        <el-button type="primary" @click="borrowList()">搜索</el-button>
        <el-button
          @click="
            readerId = '';
            bookId = '';
            state = '';
            borrowList();
          "
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 借阅列表 -->
    <el-table :data="borrow" style="width: 100%">
      <el-table-column label="序号" type="index"> </el-table-column>
      <el-table-column label="借阅人编号" prop="readerId"></el-table-column>
      <el-table-column label="图书编号" prop="bookId"></el-table-column>
      <el-table-column label="应归还日期" prop="returnDate"> </el-table-column>
      <el-table-column label="状态" prop="status"></el-table-column>

      <el-table-column label="操作" width="200" prop="state1">
        <template #default="{ row }">
          <el-button type="info" @click="dialogVisible=true;title = '查看';showDialog(row)">查看</el-button>
          <el-button type="primary" :disabled="isAble(row)" @click="dialogVisible=true;title = '还书';showDialog(row);"
            >还书</el-button
          >
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />

    <!-- 抽屉 -->
    <el-drawer
      v-model="visibleDrawer"
      title="借阅"
      direction="rtl"
      size="30%"
    >
      <!-- 添加表单 -->
      <el-form
        :model="borrowModel"
        label-width="100px"
        :rules="rules"
      >                                                            
        <el-form-item label="借阅人编号" prop="readerId">
          <el-input
            v-model="borrowModel.readerId"
            placeholder="请输入借阅人编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="图书编号" prop="bookId">
          <el-input
            v-model="borrowModel.bookId"
            placeholder="请输入图书编号"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="checkModel" label-width="150px">
        <el-form-item>
          <el-button
            type="primary"
            @click="checkBorrow();dbutt=false;"
            
            >核验</el-button
          >

        </el-form-item>
      </el-form>
      <el-form :model="checkModel" label-width="100px" :disabled="true">
        <el-form-item label="姓名">
          <el-input v-model="checkModel.readerName"></el-input>
        </el-form-item>
        <el-form-item label="书名">
          <el-input v-model="checkModel.bookname"></el-input>
        </el-form-item>

        <el-form-item label="归还日期">
          <el-input v-model="checkModel.returnDate"></el-input>
        </el-form-item>

        <el-form-item label="已借阅数量">
          <el-input v-model="checkModel.total"></el-input>
        </el-form-item>

        <el-form-item label="余额">
          <el-input-number
            v-model="checkModel.balance"
            :precision="1"
            :step="0.1"
          />
        </el-form-item>
      </el-form>
      <el-form :model="checkModel" label-width="100px">
        <el-form-item>
          <el-button type="primary" @click="addBorrow()" :disabled=dbutt>确认</el-button> 
          <el-button @click="visibleDrawer = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>


    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form
        :model="borrowModel"
        label-width="100px"
        style="padding-right: 30px"
        :disabled="true"
      >
        <el-form-item label="借阅人编号" prop="name">
          <el-input
            v-model="borrowModel.readerId"
          ></el-input>
        </el-form-item>
        <el-form-item label="图书编号" prop="name">
          <el-input
            v-model="borrowModel.bookId"
          ></el-input>
        </el-form-item>

      <el-form :model="checkModel" label-width="100px" style="padding-right: 30px" :disabled="true">
        <el-form-item label="姓名">
          <el-input v-model="checkModel.readerName"></el-input>
        </el-form-item>
        <el-form-item label="书名">
          <el-input v-model="checkModel.bookname"></el-input>
        </el-form-item>

        <el-form-item label="归还日期">
          <el-input v-model="checkModel.returnDate1"></el-input>
        </el-form-item>

        <el-form-item label="应缴纳费用">
          <el-input-number
            v-model="checkModel.balance1"
            :precision="1"
            :step="0.1"
          />
        </el-form-item>
      </el-form>
      
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false;">取消</el-button>
          <el-button
            type="primary"
            @click="returnBook()"
            :disabled="title == '查看' ? true : false"
          >
            确认还书
          </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>


</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>